﻿using System;
using System.Configuration;
using FriendlyURL.Web.Domain.Abstract;

namespace FriendlyURL.Web.Domain.Impl
{
    public class Configuration : IConfiguration
    {
        private ICache _cache;
        private IMessageHandler _messageHandler;

        /// <summary>
        /// Constructor for the Configuration class
        /// </summary>
        public Configuration()
        {
            _cache = DIFactory.CreateCacheType();
            _messageHandler = DIFactory.CreateMessageHandlerType();
        }

        /// <summary>
        /// GoogleAnalyticsAccount
        /// <remarks>
        /// Used to retreive the GoogleAnalyticsAccount used to track usage information for this application
        /// </remarks>
        /// </summary>
        public string GoogleAnalyticsAccount
        {
            get
            {
                return (string)getAppSetting(typeof(string), "GoogleAnalyticsAccount");
            }
        }

        /// <summary>
        /// SiteTitle
        /// <remarks>
        /// Used to retreive the name of this application
        /// </remarks>
        /// </summary>
        public string SiteTitle
        {
            get
            {
                return (string)getAppSetting(typeof(string), "SiteTitle");
            }
        }

        /// <summary>
        /// ErrorLogFilePath
        /// <remarks>
        /// Used to retreive the Error Log File Path for this application
        /// </remarks>
        /// </summary>
        public string ErrorLogFilePath
        {
            get
            {
                return (string)getAppSetting(typeof(string), "ErrorLogFilePath");
            }
        }


        #region Private Methods

        private object getAppSetting(Type expectedType, string key)
        {
            string value = ConfigurationManager.AppSettings.Get(key);
            if (value == null)
            {
                _messageHandler.HandleMessage("Error in Configuration.cs: " + string.Format("AppSetting: {0} is not configured", key), GlobalProperties.LogErrorName, true);
                throw new Exception(string.Format("AppSetting: {0} is not configured.", key));
            }
            try
            {
                if (expectedType.Equals(typeof(int)))
                {
                    return int.Parse(value);
                }
                if (expectedType.Equals(typeof(string)))
                {
                    return value;
                }
                if (expectedType.Equals(typeof(bool)))
                {
                    if (value.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                throw new Exception("Type not supported.");
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Config key:{0} was expected to be of type {1} but was not.", key, expectedType), ex);
            }
        }

        #endregion
    }
}
